<!DOCTYPE html>
<html>
<head>
    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?a7c05ce530152d9866930ef4850ee566"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    
    
    
    <title>Struts2基本执行过程的理解 | 神奇的鸭鸭の码农库 | 新知识要不断的总结记录成笔记，要多写，多画，能够清晰透彻的将知识讲给别人听，才是达到理解的层次。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#765959">
    
    
    <meta name="keywords" content="源码">
    <meta name="description" content="Struts2基本执行过程的理解以下是在学习过程中，对struts2执行过程粗略的理解，可能存在大量误解，请勿用于学习。 先上struts2的框架结构图  1、StrutsPrepareAndExecuteFilter核心过滤器首先访问[http://localhost:8080/struts2-demo1/aaa/HelloAction.do]，此时会进入StrutsPrepareAndExec">
<meta name="keywords" content="源码">
<meta property="og:type" content="article">
<meta property="og:title" content="Struts2基本执行过程的理解">
<meta property="og:url" content="http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/index.html">
<meta property="og:site_name" content="神奇的鸭鸭の码农库">
<meta property="og:description" content="Struts2基本执行过程的理解以下是在学习过程中，对struts2执行过程粗略的理解，可能存在大量误解，请勿用于学习。 先上struts2的框架结构图  1、StrutsPrepareAndExecuteFilter核心过滤器首先访问[http://localhost:8080/struts2-demo1/aaa/HelloAction.do]，此时会进入StrutsPrepareAndExec">
<meta property="og:image" content="http://or4fl5xux.bkt.clouddn.com/Struts2-Architecture_%E6%96%B0%E7%89%88%E6%9C%AC.png">
<meta property="og:updated_time" content="2017-12-05T10:00:04.803Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Struts2基本执行过程的理解">
<meta name="twitter:description" content="Struts2基本执行过程的理解以下是在学习过程中，对struts2执行过程粗略的理解，可能存在大量误解，请勿用于学习。 先上struts2的框架结构图  1、StrutsPrepareAndExecuteFilter核心过滤器首先访问[http://localhost:8080/struts2-demo1/aaa/HelloAction.do]，此时会进入StrutsPrepareAndExec">
<meta name="twitter:image" content="http://or4fl5xux.bkt.clouddn.com/Struts2-Architecture_%E6%96%B0%E7%89%88%E6%9C%AC.png">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">神奇的鸭鸭</h5>
          <a href="mailto:702038338@qq.com" title="702038338@qq.com" class="mail">702038338@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/magic_duck" target="_blank" >
                <i class="icon icon-lg icon-gg-circle"></i>
                码云
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.zhihu.com/people/shen-qi-de-ya-ya" target="_blank" >
                <i class="icon icon-lg icon-twitter"></i>
                知乎
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/404.html"  >
                <i class="icon icon-lg icon-link"></i>
                404页面测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Struts2基本执行过程的理解</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Struts2基本执行过程的理解</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-06-05T16:00:00.000Z" itemprop="datePublished" class="page-time">
  2017-06-06
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Struts2基本执行过程的理解"><span class="post-toc-number">1.</span> <span class="post-toc-text">Struts2基本执行过程的理解</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#先上struts2的框架结构图"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">先上struts2的框架结构图</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#1、StrutsPrepareAndExecuteFilter核心过滤器"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">1、StrutsPrepareAndExecuteFilter核心过滤器</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#2、进入调度器Dispatcher-java"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">2、进入调度器Dispatcher.java</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#3、在执行动作方法前，要执行一些拦截器，所以proxy-execute方法里执行了invocation-invoke-方法。"><span class="post-toc-number">1.4.</span> <span class="post-toc-text">3、在执行动作方法前，要执行一些拦截器，所以proxy.execute方法里执行了invocation.invoke()方法。</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#至此，struts2基本执行流程结束"><span class="post-toc-number">1.5.</span> <span class="post-toc-text">至此，struts2基本执行流程结束</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-Struts2执行过程的粗略理解"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Struts2基本执行过程的理解</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年06月06日 0:00" datetime="2017-06-05T16:00:00.000Z"  itemprop="datePublished">2017-06-06</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="Struts2基本执行过程的理解"><a href="#Struts2基本执行过程的理解" class="headerlink" title="Struts2基本执行过程的理解"></a>Struts2基本执行过程的理解</h2><p>以下是在学习过程中，对struts2执行过程粗略的理解，可能存在大量误解，请勿用于学习。</p>
<h3 id="先上struts2的框架结构图"><a href="#先上struts2的框架结构图" class="headerlink" title="先上struts2的框架结构图"></a>先上struts2的框架结构图</h3><p><img src="http://or4fl5xux.bkt.clouddn.com/Struts2-Architecture_%E6%96%B0%E7%89%88%E6%9C%AC.png" alt="Struts2-Architecture_新版本"></p>
<hr>
<h3 id="1、StrutsPrepareAndExecuteFilter核心过滤器"><a href="#1、StrutsPrepareAndExecuteFilter核心过滤器" class="headerlink" title="1、StrutsPrepareAndExecuteFilter核心过滤器"></a>1、StrutsPrepareAndExecuteFilter核心过滤器</h3><p>首先访问[<a href="http://localhost:8080/struts2-demo1/aaa/HelloAction.do]，此时会进入StrutsPrepareAndExecuteFilter过滤器，因为我们在web.xml中配置了它，StrutsPrepareAndExecuteFilter是一个普通的Filter类，因此关心它的doFilter方法，源码如下：" target="_blank" rel="external">http://localhost:8080/struts2-demo1/aaa/HelloAction.do]，此时会进入StrutsPrepareAndExecuteFilter过滤器，因为我们在web.xml中配置了它，StrutsPrepareAndExecuteFilter是一个普通的Filter类，因此关心它的doFilter方法，源码如下：</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doFilter</span><span class="params">(ServletRequest req, ServletResponse res, FilterChain chain)</span> <span class="keyword">throws</span> IOException, ServletException </span>&#123;</div><div class="line">	<span class="comment">//父类转子类</span></div><div class="line">	HttpServletRequest request = (HttpServletRequest) req;</div><div class="line">	HttpServletResponse response = (HttpServletResponse) res;</div><div class="line"></div><div class="line">	<span class="keyword">try</span> &#123;</div><div class="line">		<span class="comment">//此处做一些工作，如设置编码和国际化，创建Action上下文(map)</span></div><div class="line">		prepare.setEncodingAndLocale(request, response);</div><div class="line">		prepare.createActionContext(request, response);</div><div class="line">		prepare.assignDispatcherToThread();</div><div class="line">		<span class="comment">//此处应该是判断是否是需要处理的路径，不是直接放行</span></div><div class="line">		<span class="keyword">if</span> (excludedPatterns != <span class="keyword">null</span> &amp;&amp; prepare.isUrlExcluded(request, excludedPatterns)) &#123;</div><div class="line">			chain.doFilter(request, response);</div><div class="line">		&#125; <span class="keyword">else</span> &#123;</div><div class="line">			<span class="comment">//此处对request进行了包装，底层有两种request包装类型</span></div><div class="line">			<span class="comment">//1 如果是multipart/form-data类型 返回MultiPartRequestWrapper</span></div><div class="line">			<span class="comment">//2 否则返回 StrutsRequestWrapper</span></div><div class="line">			request = prepare.wrapRequest(request);</div><div class="line">			<span class="comment">//此处到达图中的ActionMapping动作映射 用于分析请求信息</span></div><div class="line">			<span class="comment">//内部通过getMapping方法（实现类DefaultActionMapper）的parseNameAndNamespace方法对namespace和name进行了分离</span></div><div class="line">			ActionMapping mapping = prepare.findActionMapping(request, response, <span class="keyword">true</span>);</div><div class="line">			<span class="keyword">if</span> (mapping == <span class="keyword">null</span>) &#123;</div><div class="line">				<span class="keyword">boolean</span> handled = execute.executeStaticResourceRequest(request, response);</div><div class="line">				<span class="keyword">if</span> (!handled) &#123;</div><div class="line">					chain.doFilter(request, response);</div><div class="line">				&#125;</div><div class="line">			&#125; <span class="keyword">else</span> &#123;</div><div class="line">				<span class="comment">//如果不为空就执行Action方法，将请求和动作映射传入</span></div><div class="line">				<span class="comment">//下面进入Dispatcher.java调度器</span></div><div class="line">				execute.executeAction(request, response, mapping);</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">	&#125; <span class="keyword">finally</span> &#123;</div><div class="line">		prepare.cleanupRequest(request);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="2、进入调度器Dispatcher-java"><a href="#2、进入调度器Dispatcher-java" class="headerlink" title="2、进入调度器Dispatcher.java"></a>2、进入调度器Dispatcher.java</h3><p>所谓调度器就是起到一个分配工作的功能，此类被调用了serviceAction方法，其主要功能是通过action配置文件加载相应的action类，并执行适当的action方法，或者直接跳转到result。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">serviceAction</span><span class="params">(HttpServletRequest request, HttpServletResponse response, ServletContext context,</span></span></div><div class="line"><span class="function"><span class="params">						  ActionMapping mapping)</span> <span class="keyword">throws</span> ServletException </span>&#123;</div><div class="line"></div><div class="line">	<span class="comment">//创建了数据中心，保存了所有的servlet容器</span></div><div class="line">	Map&lt;String, Object&gt; extraContext = createContextMap(request, response, mapping, context);</div><div class="line"></div><div class="line">	<span class="comment">//创建了值栈，如果之前存在一个值栈就创建一个新的副本传递到新的数据中心中使用</span></div><div class="line">	<span class="comment">// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action</span></div><div class="line">	ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);</div><div class="line">	<span class="keyword">boolean</span> nullStack = stack == <span class="keyword">null</span>;</div><div class="line">	<span class="keyword">if</span> (nullStack) &#123;</div><div class="line">		ActionContext ctx = ActionContext.getContext();</div><div class="line">		<span class="keyword">if</span> (ctx != <span class="keyword">null</span>) &#123;</div><div class="line">			stack = ctx.getValueStack();</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">if</span> (stack != <span class="keyword">null</span>) &#123;</div><div class="line">		extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	<span class="comment">//处理请求的调度程序</span></div><div class="line">	String timerKey = <span class="string">"Handling request from Dispatcher"</span>;</div><div class="line">	<span class="keyword">try</span> &#123;</div><div class="line">		UtilTimerStack.push(timerKey);</div><div class="line">		<span class="comment">//从mapping中拿到动作映射的数据</span></div><div class="line">		String namespace = mapping.getNamespace();</div><div class="line">		String name = mapping.getName();</div><div class="line">		String method = mapping.getMethod();</div><div class="line"></div><div class="line">		<span class="comment">//获取当前的XWork配置对象？用来加载代理类的？</span></div><div class="line">		Configuration config = configurationManager.getConfiguration();</div><div class="line">		<span class="comment">//动作类的代理类登场了，创建的同时拿到了映射数据，之后会去读取配置文件struts.xml确定执行哪个方法</span></div><div class="line">		ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(</div><div class="line">				namespace, name, method, extraContext, <span class="keyword">true</span>, <span class="keyword">false</span>);</div><div class="line"></div><div class="line">		<span class="comment">//往request域中扔了一个值栈？</span></div><div class="line">		request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());</div><div class="line"></div><div class="line">		<span class="comment">//如果ActionMapping说直接访问一个结果就直接访问一个结果</span></div><div class="line">		<span class="comment">// if the ActionMapping says to go straight to a result, do it!</span></div><div class="line">		<span class="keyword">if</span> (mapping.getResult() != <span class="keyword">null</span>) &#123;</div><div class="line">			Result result = mapping.getResult();</div><div class="line">			result.execute(proxy.getInvocation());</div><div class="line">		&#125; <span class="keyword">else</span> &#123;</div><div class="line">			<span class="comment">//执行proxy</span></div><div class="line">			proxy.execute();</div><div class="line">		&#125;</div><div class="line"></div><div class="line">		<span class="comment">// If there was a previous value stack then set it back onto the request</span></div><div class="line">		<span class="keyword">if</span> (!nullStack) &#123;</div><div class="line">			request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);</div><div class="line">		&#125;</div><div class="line">	&#125; <span class="keyword">catch</span> (ConfigurationException e) &#123;</div><div class="line">		<span class="comment">// WW-2874 Only log error if in devMode</span></div><div class="line">		<span class="keyword">if</span> (devMode) &#123;</div><div class="line">			String reqStr = request.getRequestURI();</div><div class="line">			<span class="keyword">if</span> (request.getQueryString() != <span class="keyword">null</span>) &#123;</div><div class="line">				reqStr = reqStr + <span class="string">"?"</span> + request.getQueryString();</div><div class="line">			&#125;</div><div class="line">			LOG.error(<span class="string">"Could not find action or result\n"</span> + reqStr, e);</div><div class="line">		&#125; <span class="keyword">else</span> &#123;</div><div class="line">			<span class="keyword">if</span> (LOG.isWarnEnabled()) &#123;</div><div class="line">				LOG.warn(<span class="string">"Could not find action or result"</span>, e);</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);</div><div class="line">	&#125; <span class="keyword">catch</span> (Exception e) &#123;</div><div class="line">		<span class="keyword">if</span> (handleException || devMode) &#123;</div><div class="line">			sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);</div><div class="line">		&#125; <span class="keyword">else</span> &#123;</div><div class="line">			<span class="keyword">throw</span> <span class="keyword">new</span> ServletException(e);</div><div class="line">		&#125;</div><div class="line">	&#125; <span class="keyword">finally</span> &#123;</div><div class="line">		UtilTimerStack.pop(timerKey);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="3、在执行动作方法前，要执行一些拦截器，所以proxy-execute方法里执行了invocation-invoke-方法。"><a href="#3、在执行动作方法前，要执行一些拦截器，所以proxy-execute方法里执行了invocation-invoke-方法。" class="headerlink" title="3、在执行动作方法前，要执行一些拦截器，所以proxy.execute方法里执行了invocation.invoke()方法。"></a>3、在执行动作方法前，要执行一些拦截器，所以proxy.execute方法里执行了invocation.invoke()方法。</h3><p>拦截器可以在Action和Result被执行之前或之后进行一些处理，拦截是AOP的一种实现策略，是动态拦截Action调用的对象。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> String <span class="title">invoke</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">	String profileKey = <span class="string">"invoke: "</span>;</div><div class="line">	<span class="keyword">try</span> &#123;</div><div class="line">		UtilTimerStack.push(profileKey);</div><div class="line"></div><div class="line">		<span class="keyword">if</span> (executed) &#123;</div><div class="line">			<span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(<span class="string">"Action has already executed"</span>);</div><div class="line">		&#125;</div><div class="line"></div><div class="line">		<span class="comment">//递归执行interceptor</span></div><div class="line">		<span class="keyword">if</span> (interceptors.hasNext()) &#123;</div><div class="line">			<span class="comment">//获得拦截器</span></div><div class="line">			<span class="keyword">final</span> InterceptorMapping interceptor = interceptors.next();</div><div class="line">			String interceptorMsg = <span class="string">"interceptor: "</span> + interceptor.getName();</div><div class="line">			UtilTimerStack.push(interceptorMsg);</div><div class="line">			<span class="keyword">try</span> &#123;</div><div class="line">							<span class="comment">//每个Interceptor方法都会return invocation.invoke()，从而实现递归过程</span></div><div class="line">							resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.<span class="keyword">this</span>);</div><div class="line">						&#125;</div><div class="line">			<span class="keyword">finally</span> &#123;</div><div class="line">				UtilTimerStack.pop(interceptorMsg);</div><div class="line">			&#125;</div><div class="line">		&#125; <span class="keyword">else</span> &#123;</div><div class="line">			<span class="comment">//没有可执行的interceptor时调用唯一的action</span></div><div class="line">			resultCode = invokeActionOnly();</div><div class="line">		&#125;</div><div class="line"></div><div class="line">		<span class="comment">//在结果返回之前调用preResultListener 通过executed控制只执行一次</span></div><div class="line">		<span class="comment">//preResultListener可以在Action执行之后，返回结果集Result之前执行某些动作</span></div><div class="line">		<span class="comment">// this is needed because the result will be executed, then control will return to the Interceptor, which will</span></div><div class="line">		<span class="comment">// return above and flow through again</span></div><div class="line">		<span class="keyword">if</span> (!executed) &#123;</div><div class="line">			<span class="keyword">if</span> (preResultListeners != <span class="keyword">null</span>) &#123;</div><div class="line">				<span class="keyword">for</span> (Object preResultListener : preResultListeners) &#123;</div><div class="line">					PreResultListener listener = (PreResultListener) preResultListener;</div><div class="line"></div><div class="line">					String _profileKey = <span class="string">"preResultListener: "</span>;</div><div class="line">					<span class="keyword">try</span> &#123;</div><div class="line">						UtilTimerStack.push(_profileKey);</div><div class="line">						listener.beforeResult(<span class="keyword">this</span>, resultCode);</div><div class="line">					&#125;</div><div class="line">					<span class="keyword">finally</span> &#123;</div><div class="line">						UtilTimerStack.pop(_profileKey);</div><div class="line">					&#125;</div><div class="line">				&#125;</div><div class="line">			&#125;</div><div class="line"></div><div class="line">			<span class="comment">// now execute the result, if we're supposed to</span></div><div class="line">			<span class="keyword">if</span> (proxy.getExecuteResult()) &#123;</div><div class="line">				executeResult();</div><div class="line">			&#125;</div><div class="line"></div><div class="line">			executed = <span class="keyword">true</span>;</div><div class="line">		&#125;</div><div class="line"></div><div class="line">		<span class="keyword">return</span> resultCode;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">finally</span> &#123;</div><div class="line">		UtilTimerStack.pop(profileKey);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="至此，struts2基本执行流程结束"><a href="#至此，struts2基本执行流程结束" class="headerlink" title="至此，struts2基本执行流程结束"></a>至此，struts2基本执行流程结束</h3>
        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-12-05T10:00:04.803Z" itemprop="dateUpdated">2017年12月5日 18:00</time>
</span><br>


        如要转载请注明出处：<a href="/2017/06/06/Struts2执行过程的粗略理解/" target="_blank" rel="external">http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/</a>
    </div>
    <footer>
        <a href="http://magic_duck.oschina.io">
            <img src="/img/avatar.jpg" alt="神奇的鸭鸭">
            神奇的鸭鸭
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/源码/">源码</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/&title=《Struts2基本执行过程的理解》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/&title=《Struts2基本执行过程的理解》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Struts2基本执行过程的理解》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/06/09/cloud.tencent_Centos6.5_Install_JavaEnvironment/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">腾讯云Centos6.5搭建JavaWeb环境:Mysql5.6+jdk1.7+tomcat7</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/05/23/szpt1456/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">2017热身赛第十场——集合元素和(1456)</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我只要一角钱~ ~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://git.oschina.net/z77z" target="_blank">邹海清</a></span>
            <span>神奇的鸭鸭の码农库 &copy; 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/&title=《Struts2基本执行过程的理解》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/&title=《Struts2基本执行过程的理解》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Struts2基本执行过程的理解》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://magic_duck.oschina.io/2017/06/06/Struts2执行过程的粗略理解/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



lazyScripts.push('//s95.cnzz.com/z_stat.php?id=1261081671&web_id=1261081671')

</script>

<script src="/js/main.min.js?v=1.4.3"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.3" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
